using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using Ltf.Remoting.Server;
using Ltf.Remoting;
using System.Xml.Serialization;
using Ltf.Remoting.Settings;
using Ltf.Remoting.Commands;
using Ltf.Remoting.TFAccess;
using Ltf.Remoting.TeamFoundation;
using System.Diagnostics;
// comment
namespace Ltf.MonitorLizard.Controls
{
	public partial class LizardTFVCProps : UserControl
	{
		private string fileName;
		public LizardTFVCProps()
		{
			InitializeComponent();
		}
		public LizardTFVCProps(string fileName)
		{
			InitializeComponent();
			this.fileName = fileName;
			LoadLTFVCFile(fileName);
		}

		public void LoadFile(string fileName)
		{
			this.fileName = fileName;
			LoadLTFVCFile(fileName);
		}

		private string linkUrl = string.Empty;
		private static Ltf.Remoting.Settings.TFSettings settings;
		private void LoadLTFVCFile(string fileName)
		{
			try
			{
				string folderName = Path.GetDirectoryName(fileName);
				TFServerConnection connection = ServerConnections.GetConnectionForPath(fileName);
				string ltfvcFolder = connection.GetMetaFolder(fileName);
				string ltfvcFileBase = Path.Combine(ltfvcFolder, Path.GetFileName(fileName));
				string ltfvcFile = ltfvcFileBase + ".ltfvc";
				LoadHistory();

				LizardFileInfo fi = ServerFunctions.GetFileInfoFromFile(ltfvcFile);

				Image i = null;
				linkLabel1.Text = fi.ServerPath;
				//		linkUrl = "http://Oryx:8080/UI/Pages/Scc/ViewSource.aspx?scc-item=id%3D" + fi.ServerPath + "%26cs%3D-1";

				linkLabel1.Click += new EventHandler(linkLabel1_Click);
				if (fi.Status == LizardFileStatus.Same && fi.LockStatus == "None" && fi.ChangeType == "Edit" && fi.LocalVersion == fi.ServerVersion)
					i = imlIcons.Images["CheckedOutBig.ico"];
				if (fi.LockStatus == "None" && fi.ChangeType == "None" && fi.LocalVersion == fi.ServerVersion)
					i = imlIcons.Images["ReadOnlyBig.ico"];
				if (fi.LockStatus == "None" && fi.ChangeType == "None" && fi.LocalVersion != fi.ServerVersion)
					i = imlIcons.Images["OutOfDateBig.ico"];
				if (fi.Status == LizardFileStatus.Same && fi.LockStatus != "None")
					i = imlIcons.Images["LockedBig.ico"];
				if (fi.Status == LizardFileStatus.Different && fi.LockStatus == "None" && fi.ChangeType == "Edit" && fi.LocalVersion == fi.ServerVersion)
					i = imlIcons.Images["ModifiedBig.ico"];
				if (fi.Status == LizardFileStatus.Different && fi.LockStatus == "None" && fi.ChangeType == "Edit" && fi.LocalVersion != fi.ServerVersion)
					i = imlIcons.Images["ConflictBig.ico"];
				if (fi.Status == LizardFileStatus.Added)
					i = imlIcons.Images["AddedBig.ico"];
				pcbIcon.Image = i;
				label1.Text = string.Format("Version on server:{0}", fi.ServerVersion);
				label2.Text = string.Format("Version on workstation:{0}{1}", fi.LocalVersion, fi.Status == LizardFileStatus.Different ? " - Modified" : string.Empty);
				label3.Text = string.Format("Status :{0}", fi.ChangeType);
				label4.Text = string.Format("Locked By :{0}", fi.LockOwner);
				label5.Text = string.Format("Lizard state :{0}", fi.Status);
				label6.Text = string.Format("Other Edits :{0}", fi.HasOtherPendingChange ? "Yes" : "No");
				txtOtherEdits.Text = fi.OtherUsers;
			}
			catch (Exception ex)
			{
				linkLabel1.Text = fileName;
				linkUrl = string.Empty;
				label1.Text = "No Lizard TFVC information found for file";
				label2.Text = string.Empty;
				label3.Text = string.Empty;
				label4.Text = string.Empty;
				label5.Text = string.Empty;
				label6.Text = string.Empty;
				txtOtherEdits.Visible = false;
				pcbIcon.Visible = false;
				//			File.WriteAllText("c:\\lizarderr.txt", string.Format("{0}\r\n\r\n{1}", ex.Message, ex.StackTrace));
			}
		}

		void linkLabel1_Click(object sender, EventArgs e)
		{
			//TODO: no links yet!
		}

		private void btnHistory_Click(object sender, EventArgs e)
		{
			LoadHistory();
		}

		
		private void LoadHistory()
		{
			try
			{
				ServerFunctions sf = new ServerFunctions();
				List<LizardChangeset> History = sf.GetHistory(fileName);
				lsvHistory.Items.Clear();
				foreach (LizardChangeset change in History)
				{
					ListViewItem lvi = lsvHistory.Items.Add(change.ChangesetId.ToString()); // changeset id
					lvi.SubItems.AddRange(new string[] { change.CreationDate.ToString("dd/mm/yyyy hh:nn:ss"), change.CheckinNote, change.Commiter, change.Comment });
				}
			}

			catch
			{
			}
		}

		private void btnTFVCHistory_Click(object sender, EventArgs e)
		{
			string folderName = Path.GetDirectoryName(fileName);
			ServerFunctions sf = new ServerFunctions();
			sf.ShowHistory(fileName);
		}

		private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			Process p = new Process();
			string winDir = System.Environment.GetEnvironmentVariable("windir");

			string metaFolderPath = ServerConnections.GetConnectionForPath(fileName).GetMetaFolder(fileName);

			string explorer = Path.Combine(winDir, "explorer.exe");

			p.StartInfo.FileName = explorer;
			p.StartInfo.Arguments = "\"" + metaFolderPath.ToUpper()+ "\"";
			p.Start();

		}
	}
}
